home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / misc / pdflib / p_tiff.c < prev    next >
C/C++ Source or Header  |  1999-01-01  |  4KB  |  179 lines

  1. /* p_tiff.c
  2.  * Copyright (C) 1997-98 Thomas Merz. All rights reserved.
  3.  *
  4.  * TIFF processing for PDFlib
  5.  */
  6.  
  7. #include "p_intern.h"
  8.  
  9. #ifdef USE_TIFF
  10.  
  11. static void
  12. pdf_data_source_TIFF_init(PDF *p, PDF_data_source *src)
  13. {
  14.   PDF_image    *image;
  15.  
  16.   image = (PDF_image *) src->private_data;
  17.   image->cur_line = 0;
  18.  
  19.   src->buffer_length = image->components * image->width;
  20.   src->buffer_start = 
  21.       PDF_malloc(src->buffer_length, "PDF_data_source_TIFF_init");
  22. }
  23.  
  24. static bool
  25. pdf_data_source_TIFF_fill(PDF *p, PDF_data_source *src)
  26. {
  27.   PDF_image    *image;
  28.   int        col;
  29.   byte        *dest;
  30.   uint32    *s;
  31.  
  32.   image = (PDF_image *) src->private_data;
  33.  
  34.   if (image->cur_line++ == image->height)
  35.     return false;
  36.  
  37.   src->next_byte = src->buffer_start;
  38.   src->bytes_available = src->buffer_length;
  39.  
  40.   dest = src->buffer_start;
  41.   s = image->raster + (image->height - 1 - image->cur_line) * image->width;
  42.   
  43.   switch (image->components) {
  44.       case 1:
  45.           for (col = 0; col < image->width; col++, s++) {
  46.             *dest++ = TIFFGetR(*s);
  47.           }
  48.           break;
  49.  
  50.       case 3:
  51.           for (col = 0; col < image->width; col++, s++) {
  52.             *dest++ = TIFFGetR(*s);
  53.             *dest++ = TIFFGetG(*s);
  54.             *dest++ = TIFFGetB(*s);
  55.           }
  56.           break;
  57.  
  58.       default:
  59.         pdf_error(p, PDF_FATAL, 
  60.             "Unknown color space in TIFF image %s (%d components)", 
  61.             image->filename, image->components);
  62.   }
  63.  
  64.   return true;
  65. }
  66.  
  67. static void
  68. pdf_data_source_TIFF_terminate(PDF *p, PDF_data_source *src)
  69. {
  70.   PDF_image    *image;
  71.  
  72.   image = (PDF_image *) src->private_data;
  73.  
  74.   PDF_free((void *) src->buffer_start);
  75. }
  76.  
  77. PDF_image *
  78. PDF_open_TIFF(PDF *p, char *filename)
  79. {
  80.     PDF_image *image;
  81.     int32 w, h;
  82.     uint16 bpc;
  83.     tsample_t components;
  84.     size_t npixels;
  85.     uint16    *rmap, *gmap, *bmap;
  86.  
  87.     image = (PDF_image *) PDF_malloc(sizeof(PDF_image), "PDF_open_TIFF");
  88.  
  89.     if (image == NULL)
  90.     return NULL;
  91.  
  92.     image->tif = TIFFOpen(filename, "r");
  93.  
  94.     if (!image->tif) {
  95.     PDF_free(image);
  96.     return NULL;
  97.     }
  98.  
  99.     TIFFGetField(image->tif, TIFFTAG_IMAGEWIDTH, &w);
  100.     TIFFGetField(image->tif, TIFFTAG_IMAGELENGTH, &h);
  101.     TIFFGetField(image->tif, TIFFTAG_BITSPERSAMPLE, &bpc);
  102.     TIFFGetField(image->tif, TIFFTAG_SAMPLESPERPIXEL, &components);
  103.  
  104.     image->filename = filename;
  105.     image->width        = w;
  106.     image->height        = h;
  107.     /* we use 8 bit to retrieve the image data in all cases */
  108.     image->bpc            = 8;
  109.     image->components    = components;
  110.     image->compression    = none;
  111.     image->closefunc    = PDF_close_TIFF;
  112.     image->BitPixel        = 1 << image->bpc;
  113.     image->indexed        = false;
  114.  
  115.     switch (image->components) {
  116.     case 1:
  117.         if (TIFFGetField(image->tif, TIFFTAG_COLORMAP, &rmap, &gmap, &bmap)) {
  118.             /* if it is a palette image we have to adapt these accordingly */
  119.             image->colorspace    = DeviceRGB;
  120.             image->components    = 3;
  121.         } else
  122.             /* plain old grayscale image */
  123.             image->colorspace    = DeviceGray;
  124.         break;
  125.     case 3:
  126.         image->colorspace    = DeviceRGB;
  127.         break;
  128.     case 4:
  129.         image->colorspace    = DeviceCMYK;
  130.         break;
  131.     default:
  132.         pdf_error(p, PDF_FATAL, 
  133.         "Unknown color space in TIFF image %s (%d components)", 
  134.         image->filename, image->components);
  135.         
  136.     }
  137.  
  138.     image->src.init        = pdf_data_source_TIFF_init;
  139.     image->src.fill        = pdf_data_source_TIFF_fill;
  140.     image->src.terminate    = pdf_data_source_TIFF_terminate;
  141.     image->src.private_data    = (void *) image;
  142.  
  143.     npixels = w * h;
  144.     image->raster = (uint32*) _TIFFmalloc(npixels * sizeof (uint32));
  145.  
  146.     if (image->raster == NULL ||
  147.     !TIFFReadRGBAImage(image->tif, w, h, image->raster, 0)) {
  148.     TIFFClose(image->tif);
  149.     PDF_free(image);
  150.     return NULL;
  151.     }
  152.  
  153.     return image;
  154. }
  155.  
  156. void
  157. PDF_close_TIFF(PDF *p, PDF_image *image)
  158. {
  159.     _TIFFfree(image->raster);
  160.     TIFFClose(image->tif);
  161.     PDF_free(image);
  162. }
  163.  
  164. #else    /* USE_TIFF */
  165.  
  166. /* Define dummies if we can't use TIFFlib */
  167. PDF_image *
  168. PDF_open_TIFF(PDF *p, char *filename)
  169. {
  170.     return false;
  171. }
  172.  
  173. void
  174. PDF_close_TIFF(PDF *p, PDF_image *image)
  175. {
  176. }
  177.  
  178. #endif    /* USE_TIFF */
  179.